<template>
	<view class="content">
		<uni-card :is-shadow="false" padding="10px 0">
			<uni-section title="进度计划任务" type="line"></uni-section>
			<view>区域：{{dataInfo.region}}</view>
			<view>楼层：{{dataInfo.floor}}</view>
			<view>工艺名称：{{dataInfo.title}}</view>
			<view>进度责任人:{{dataInfo.user.name}}</view>
		</uni-card>
		
		<uni-card v-if="baseFormData.type == 1 || baseFormData.type == 3" :is-shadow="false" padding="10px 0">
			<uni-section title="最新变更" type="line"></uni-section>
			<uni-row class="p15">
				<uni-col :span="24">
					<view>操作类型：<span>{{type[baseFormData.type]}}</span></view>
				</uni-col>
				<uni-col :span="24">
					<view>原来时间：{{dataInfo.start_date}}</view>
				</uni-col>
			</uni-row>
		</uni-card>
		
		<uni-card class="forms" v-if="baseFormData.type == 2 || baseFormData.type == 4" :is-shadow="false" padding="10px 0">
			<uni-forms ref="baseForm" :modelValue="baseFormData" label-position='top'>
				<uni-forms-item label="操作选择" required>
					<uni-data-checkbox v-model="baseFormData.type" :localdata="form.type == 'start' ? start_type : end_type" />
				</uni-forms-item>
				<uni-forms-item label="责任方" required>
					<uni-data-checkbox @change="onSelectRole" v-model="baseFormData.role_id" :localdata="dataInfo.roleUsers" />
				</uni-forms-item>
				<uni-forms-item label="负责人" required>
					<uni-data-select v-model="baseFormData.person_id" :localdata="user" ></uni-data-select>
				</uni-forms-item>
				<view v-if="baseFormData.type  == 0">
					<uni-forms-item label="推迟原因" required>
						<uni-easyinput type="textarea" v-model="baseFormData.delay" placeholder="推迟原因" />
					</uni-forms-item>
					<uni-forms-item label="推迟时间" required>
						<uni-datetime-picker type="date" :clear-icon="false" v-model="baseFormData.delay_date" />
					</uni-forms-item>
					<uni-forms-item label="解决措施" required>
						<uni-easyinput type="textarea" v-model="baseFormData.measure" placeholder="解决措施" />
					</uni-forms-item>
					<uni-forms-item label="协调人">
						<uni-data-select v-model="baseFormData.harmonize_id" :localdata="user" ></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="协调内容">
						<uni-easyinput type="textarea" v-model="baseFormData.harmonize" placeholder="协调内容" />
					</uni-forms-item>
				</view>
			</uni-forms>
			<button class="mt15" v-if="is_save" type="primary" @click="submit('baseForm')">提交</button>
		</uni-card>
		
	</view>
</template>

<script>
import func from '../../../api/func'
import request from '../../../api/request'

	export default {
		data() {
			return {
				form:{},
				status: 0,
				dataInfo:{
					user:{name:''},
					roleUsers:[],
				},
				is_save:false,
				baseFormData: {
					id:0,
					type: 0,
					role_id: 0,
					person_id:0,
					delay:'',
					delay_date:'',
					measure:'',
					is_delay:true,
					harmonize_id:0,
					harmonize:''
				},
				type:['','准时开始','推迟开始','准时完成','推迟完成'],
				start_type:[{text: '准时开始',value: 1}, {text: '推迟开始',value: 2}],
				end_type:[ {text: '准时完成',value: 3}, {text: '推迟完成',value: 4}],
				user:[]
			}
		},
		onLoad(options){
			this.form.id = options.id
			this.baseFormData.schedule_id = options.id
			this.form.type = options.type
			this.dataInfo.id = options.id
			this.onGetData()
		},
		methods: {
			onGetData(){
				let _this = this
				request('schedule/'+this.dataInfo.id,'GET').then(res => {
					if(res.code != 200){
						uni.showToast({title: res.msg,icon: 'error',duration: 2000})
						return;
					}
					_this.dataInfo = res.data
					_this.dataInfo.roleUsers = func.map_uniapp_name_arr(_this.dataInfo.roleUsers)
					_this.dataInfo.roleUsers.forEach((e) => {
						e.users = func.map_uniapp_name_arr(e.users)
					})
					_this.user = _this.dataInfo.roleUsers[0].users
					if(_this.dataInfo.records){
						_this.dataInfo.records.forEach(function(e) {
							if(_this.form.type == 'start' && e.type <= 2){
								_this.baseFormData = e
							}
							if(_this.form.type == 'start' && e.type == 3){
								_this.is_save = true
							}
							if(_this.form.type == 'end' && e.type >= 3){
								_this.baseFormData = e
							}
							if(_this.form.type == 'end' && e.type == 4){
								_this.is_save = true
							}
						});
						console.log(_this.baseFormData)
					}
				}).catch(err => {
					console.log(err)
				})
			},
			onSelectRole(data){
				let _this = this
				_this.dataInfo.roleUsers.forEach((e) => {
					if(data.detail.value == e.id){
						this.user = func.map_uniapp_name_arr(e.users)
					}
				})
			},
			onLink(url){
				uni.navigateTo({
					url: '/pages/'+url
				});
			},
			submit(){
				let _this = this
				request('schedule/record','PUT',this.baseFormData).then(res => {
					if(res.code != 200){
						uni.showToast({title: res.msg,icon: 'error',duration: 2000})
						return;
					}
					_this.onLink('schedules/schedules')
				}).catch(err => {
					console.log(err)
				})
			},
		}
	}
</script>

<style>
.forms{min-height: 60vh;}
</style>
